#!/usr/bin/env python
# -*- coding: utf-8 -*-

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch

import urllib
from BeautifulSoup import BeautifulSoup

style = """<style> 
    body{background:#fff;color:#000;margin:3px 8px}#gbar{height:22px;padding-left:2px}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}#gbi,#gbs{background:#fff;left:0;position:absolute;top:24px;visibility:hidden;z-index:1000}#gbi{border:1px solid;border-color:#c9d7f1 #36c #36c #a2bae7;z-index:1001}#guser{padding-bottom:7px !important}#gbar,#guser{font-size:13px;padding-top:1px !important}@media all{.gb1,.gb3{height:22px;margin-right:.73em;vertical-align:top}#gbar{float:left}}.gb2{display:block;padding:.2em .5em}a.gb1,a.gb2,a.gb3{color:#00c !important}.gb2,.gb3{text-decoration:none}a.gb2:hover{background:#36c;color:#fff !important}.ts{border-collapse:collapse}.ts td{padding:0}.ti,.bl,form,#res h3{display:inline}.ti{display:inline-table}.fl:link,.gl a:link{color:#77c}a:link,.w,#prs a:visited,#prs a:active,.q:active,.q:visited{color:#00c}.mblink:visited,a:visited{color:#551a8b}a:active{color:red}.cur{color:#a90a08;font-weight:bold}.b{font-weight:bold}.j{width:42em;font-size:82%}.s{max-width:42em}.j font[size="-1"],.e .j{font-size:100%}.sl,.e .j font[size="-1"]{font-size:82%}#gb{text-align:right;padding:1px 0 7px;margin:0}.hd{position:absolute;width:1px;height:1px;top:-1000em;overflow:hidden}.f,.m,#tads h2,#mbEnd h2{color:#676767}.a,cite,.cite,.cite:link{color:green;font-style:normal}#mbEnd{float:right}h1,ol{margin:0;padding:0}li.g,body,html,.std,#tads h2,#mbEnd h2,h1{font-size:small;font-family:arial,sans-serif}#tads h2,#mbEnd h2,h1{font-weight:normal}#ssb,.clr{clear:both}#nav a,#nav a:visited,.blk a{color:#000}#nav .b a,#nav .b a:visited{color:#00c;font-size:medium}.ch{cursor:pointer}h3,.med{font-size:medium;font-weight:normal;padding:0;margin:0}.e{margin:.75em 0}.slk td{padding-left:40px;padding-top:5px;vertical-align:top}.slk div{padding-left:10px;text-indent:-10px}#mbEnd cite{display:block;text-align:left}#mbEnd p{margin:-.5em 0 0 .5em;text-align:center}#bsf,#ssb,.blk{border-top:1px solid #6b90da;background:#f0f7f9}#bsf,#ssb{margin:11px 0}#bsf{border-bottom:1px solid #6b90da}#ssb div{float:left;padding:4px 0 0;padding-left:4px;padding-right:.5em}#prs a,#prs b{margin-right:.6em}#ssb p{text-align:right;white-space:nowrap;margin:.1em 0;padding:.2em}#ssb{margin-top:0;padding:.1em}#mbEnd{background:#fff;padding:0;border-left:10px solid #fff;border-spacing:0;white-space:nowrap}#res{padding-right:1em}#tads{background:#fff8dd}#tads li{padding:0 3px 0 5px;margin:0}#tads .tam,#tads .tal{padding-top:12px}#mbEnd li{margin:1em 0;padding:0}.xsm{font-size:x-small}.sm{margin:0 0 0 40px;padding:0}ol li{list-style:none}.sm li{margin:0}.gl,#bsf a,.nobr,#brs a{white-space:nowrap}#mbEnd .med{white-space:normal}.sl,.r{display:inline;font-weight:normal;margin:0}.r{font-size:medium}h4.r{font-size:small}em{font-weight:bold;font-style:normal}em,b{text-decoration:inherit}body{font-family:}li.g,#res,#side_section,html,.std,#tads h2,#mbEnd h2,h1{font-family:援대┝,�뗭�,arial,sans-serif}#res{padding:1px 0 0 20px}#brs .med{font-size:13px}#brs caption{padding:0 0 5px!important}#brs td{padding:0 34px 2px 0!important}a.fl:link,a.fl:visited,a.obl:link,a.obl:visited,.sm a:link,.sm a:visited,.gl a:link,.gl a:visited{text-decoration:none}a.fl:hover,a.fl:active,.gl a:hover,.gl a:active{text-decoration:underline}a.obl:hover,a.obl:active,.sm a:hover,.sm a:active{text-decoration:none;border-bottom:1px solid}a>b{text-decoration:none}html>body a.fl:hover,.l,.ta a,.ra a,a.rsl,.r a,.gl a:hover{text-decoration:none;border-bottom:1px solid}#sd{font-size:13px;font-weight:bold}div .j,.std,li.g,.s{font-size:13px;line-height:19px}.s{max-width:49em}cite,.cite,.a,.tc label,.f,.bl,.t a,.gl,.fl,#mbEnd .fl{font-size:12px}.b a{font-size:14px}.t a.q{font-size:13px}#tsf{line-height:normal}#tsf label{font-size:12px}#tsf .xsm{font-size:11px;font-family:dotum}.t{padding:5px 10px 4px 5px}.fl,#mbEnd .fl,.gl{border-bottom-style:none;text-decoration:none}#mbEnd .fl:hover,#mbEnd .fl:active{text-decoration:underline}#spl{padding:1px 9px 0 0!important}.e{font-size:13px;line-height:2em}p.e .f{font-size:13px;line-height:2em;font-weight:bold}p.e{margin:1em 0 0}#navbar{margin-top:0;padding-top:18px}.g{margin:6px 0 17px}div,#mbEnd,.ob td{line-height:1.55}#tads h3,.r,#aob td,a.rsl{font-size:13px;line-height:19px}#tads li{margin-left:15px}#mbEnd h3{font-size:13px;line-height:21px}#tads .tam,#tads .tal{padding-top:13px}#mbEnd cite{line-height:20px}#mbEnd .std{font-size:12px;line-height:18px}#trev{margin-bottom:15px}.g .head{color:#333;margin:10px 0 0 -10px}.foot{text-align:right;margin-left:-20px;border-bottom:1px solid #c9d7f1;padding-bottom:4px}.g .foot{margin-top:12px}.r .xsm{color:#333;font-size:12px;margin-right:6px}#side_section #mbEnd{float:none}#side_section{background:#fff;margin-bottom:50px}.rhs_side_section{margin:5px 0 4px;margin-left:10px;width:17em}.rhs_side_section h2.r{display:block;margin:.5em 0 0}.rhs_side_section .g{padding:0;margin:0 0 12px}.rhs_foot{margin:5px 0 0;padding:0}.rhs_head{position:relative;color:#333;font-weight:bold;line-height:21px;font-size:13px}.rhs_head b{margin-left:0}.rhs_space{height:23px}.thumb{float:left;position:relative}.thumb img{margin-bottom:5px}.rhs_txt{font-size:13px;margin:-1px 0 0 89px}.rhs_txt a{line-height:17px}#rhs_image .rhs_head{margin-bottom:2px}#rhs_image .fd{border:1px solid #ebebeb}#rhs_image .ts{margin:-10px 0 0 -10px;text-align:center;border-collapse:separate;border-spacing:10px}#rhs_video .f{display:block;line-height:18px}#rhs_video .a{display:block;line-height:14px;margin-top:-2px}#rhs_video .g{margin:0;padding-top:2px}#rhs_video .rhs_head{margin-bottom:1px}#mbEnd .fl{display:block;text-align:left;margin:-3px 0 0 -5px}#mbEnd h2{display:block;padding-top:6px!important;margin-bottom:2px!important}#mbEnd{margin-bottom:26px!important}#topform td,#topform a{line-height:1.2}#gdst{width:55%}#gdsf{height:3.5em}.clear{font-size:.01em;clear:both;padding:0;margin:0}#rhsline{width:9px;border-right:1px solid #c9d7f1}
</style>"""

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html;charset=UTF-8'
        #self.response.out.write("한글 Okay?")
        
        result = urlfetch.fetch("http://www.google.co.kr/search?q=윤종신+mp3+site:tistory.com")
        if result.status_code == 200:
            soup = BeautifulSoup( unicode(result.content, 'euc-kr').encode('utf-8') )
            soup.prettify()
            items = soup('li')
            for item in items:
                self.response.out.write(item)
        else:
            print result


application = webapp.WSGIApplication(
                                       [('/', MainPage)],
                                       debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
    
#EOF